<template>
    <div >
         <div class="activity-right import-goods"  style="padding-top:20px;">
            <h3>导入商品</h3>
            <div class="import" style="width:50%; height:300px; margin-top:60px; box-sizing:border-box;padding:40px; border:1px solid #eee; border-radius:20px">
                <section style="height:300px;">
                    <el-upload
                    class="upload-demo"
                    ref="upload"
                    :action = 'action'
                    :headers= headers
                    :on-change="handleChange"
                    :on-remove="handleRemove"
                    :on-exceed="handleExceed"
                    :on-success = "handleSuccess"
                    :on-error = "handleError"
                    :file-list="fileList"
                    accept = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                    :limit = "1"
                    :auto-upload="true">
                        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传excel文件，每次仅能上传一个文件</div>
                    </el-upload> 
                </section>
            </div>
        </div>
    </div>
</template>
<script>
    import {addGoodsByExcel, downloadExcel} from "../../data/postData";
    import axios from 'axios'
    export default {
        data() {
            return {
                user: JSON.parse(localStorage.getItem('ddaigoUser')), 
                fileList: [],
                action:'http://ppe.dingdian.xin/ddaigo-platform/fileupload/addGoodsByExcel',//测试环境
               // action:'http://api.dingdian.xin/ddaigo-platform/fileupload/addGoodsByExcel',//生产环境
                url : '',
                headers:{
                    sessionId: JSON.parse(localStorage.getItem('ddaigoUser')).sessionId
                }//form表单请求头
            };
        },
        mounted(){
            console.log(addGoodsByExcel,'addGoodsByExcel')
        },
        methods: {
            //上传后台
            submitUpload() {
                var params = {
                    "data":{
                        "file": this.fileList
                    }
                }
            },
            //删除
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            //选择文件
            handleChange(file,fileList) {
                
            },
            //提交成功函数
            handleSuccess(res,files){
                console.log(res,'res')
                if(!res.status || res.code != 200){
                    this.$message({
                        message: res.message,
                        type: "error",
                        duration: 1000
                    })
                    console.log(files,'----files----')
                    this.$fetch.post(downloadExcel,{data:files.name}).then(res=>{
                        console.log(res)
                    })
                }else{
                    this.$message({
                        message: res.message,
                        type: "success",
                        duration: 1000
                    })
                    // this.$fetch.post(downloadExcel,{data:res.data}).then(res=>{
                    //     console.log(res)
                    // })
                    var instance = axios.create({     
                        responseType: 'blob', //返回数据的格式，可选值为arraybuffer,blob,document,json,text,stream，默认值为json
                    })
                    instance.post('http://ppe.dingdian.xin/ddaigo-platform/fileupload/downloadExcel',{
                        data:res.data
                    }).then(res=>{
                        console.log(res)
                        let data = res.data.data
                        var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'}); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
                    　　var downloadElement = document.createElement('a');
                    　　var href = window.URL.createObjectURL(blob); //创建下载的链接
                    　　downloadElement.href = href;
                    　　downloadElement.download = '123.xlsx'; //下载后文件名
                    　　document.body.appendChild(downloadElement);
                    　　downloadElement.click(); //点击下载
                    　　document.body.removeChild(downloadElement); //下载完成移除元素
                    　　window.URL.revokeObjectURL(href); //释放掉blo
                    })

                }
            },
            //提交失败
            handleError(res){
                console.log(res, 'error')
            },
            handleExceed(files, fileList) {
                console.log(files.length + fileList.length)
                this.$message.warning(`当前限制选择 1 个文件`);
            },
            handleDownload(){
                var eleForm = document.createElement("form")
                eleForm.setAttribute("action","https://codeload.github.com/douban/douban-client/legacy.zip/master")
                document.body.appendChild(eleForm);
                eleForm.submit()
            }
                    
        }
    }
</script>

